import { Node } from "cc";
import {IGameComponent, IGameLogic, IGameSystem} from "./interface/ecsEnterface"

export class GameSystem implements IGameSystem{
    private _owner:IGameComponent;
    private _node:Node;
    get NAME(): string {throw new Error("Method not implemented.");}
    get owner(): IGameComponent {return this._owner;}
    get node(): Node {return this._node}

    private _logicList:IGameLogic[]=[]

    add<T extends IGameLogic>(logic: new () => T):IGameLogic {
        let _logic:IGameLogic= new logic(); 
        _logic["_owner"]=this
        _logic["_node"]=this.node
        _logic.init();
        this._logicList.push(_logic);
        return _logic
    } 
    get(name: string): IGameLogic {
        for(let i=0;i<this._logicList.length;i++){
            if(this._logicList[i].NAME == name){
                return this._logicList[i]
            }
        }
        return null
    }
    remove(name: string) {
        for(let i=0;i<this._logicList?.length;i++){
            if(this._logicList[i].NAME == name){
                this._logicList[i] && this._logicList[i].onDestroy && this._logicList[i].onDestroy()
                this._logicList[i]=null
                this._logicList.splice(i,1)
                break
            }
        }
    }
    removeAll(){
        for(let i=0;i<this._logicList?.length;i++){
            this.remove(this._logicList[i].NAME);
            i--;
        }
    }
    init(){

    }
    update(dt:number){
        if(this._logicList){
            for(let i=0;i<this._logicList?.length;i++){
                this._logicList[i] && this._logicList[i].update(dt)
            }
        }
        
    }
    lateUpdate(dt:number){
        if(this._logicList){
            for(let i=0;i<this._logicList?.length;i++){
                this._logicList[i] && this._logicList[i].lateUpdate(dt)
            }
        }
    }

    /**
     * websocket 请求
     * @param msg       请求消息
     * @param data      请求数据
     * @param callback  请求完成回调
     * 
     */
    send(msg:string,data:any,callback?:()=>void){
        this.owner.send(msg,data,callback)
    }
    /**
     * http请
     * @param path  url 请求地址
     * @param data  请求数据
     * @param type  请求类型
     * 
     */
    request(path:string,data:any,type?:string):Promise<any>{
        return this.owner.request(path,data,type)
    }

    /**
     * 添加消息监听
     * @param msg 
     * @param callFun 
     * @param caller 
     */
    addMsg(msg:string,callFun:any,caller:any){
        this.owner.addMsg(msg,callFun,caller)
    }
    /**
     * 发送消息
     * @param msg 
     * @param params 
     */
    sendMsg(msg:string,...params:any){
        this.owner.sendMsg(msg,...params)
    }
    /**
     * 删除消息监听
     * @param msg 
     * @param callFun 
     * @param caller 
     */
    removeMsg(msg:string,callFun:any,caller:any){
        this.owner.removeMsg(msg,callFun,caller)
    }
     /**
     * 删除当前组消息
     * @param group 
     */
    removeGroup(){
        this.owner.removeGroup()
    }
    
    /** 将消息添加到全局消息模块中 */
    addWorld(msg:string,callFun:any,caller:any){this.owner.addWorld(msg,callFun,caller)}
    /** 发送全局消息 */
    sendWorld(msg:string,...params){this.owner.sendWorld(msg,...params)}
    /** 删除全局消息监听 */
    removeWorld(msg:string,callFun:any,caller:any){this.owner.removeWorld(msg,callFun,caller)}

}